# -*- coding: utf-8 -*-
import json
from functools import wraps
from flask import make_response, request, current_app, Response


class JsonResult():
    @staticmethod
    def getResponse(obj):
        """
            封装返回的json数据
        """
        headers = {
            'content-type': 'application/json'
        }
        response = make_response(str(obj))
        response.headers = headers
        return response

    @staticmethod
    def success(data):
        """
        成功返回
        :param data:
        :return:
        """
        result = {}
        result['code'] = 200
        result['data'] = data
        return result

    @staticmethod
    def fail(data):
        """
        失败返回
        :param data:
        :return:
        """
        result = {}
        result['code'] = 500
        result['data'] = data
        return result





def support_jsonp(f):
    """
    自定义装饰器
    Wraps JSONified output for JSONP or json """
    @wraps(f)
    def decorated_function(**kwargs):
        if 'callback' in request.values:
            content = str(request.values['callback']) + '(' + str(f(**kwargs)) + ')'
            return current_app.response_class(content, mimetype='application/javascript;charset=utf-8')
        else:
            #return Response(json.dumps(f(**kwargs)),content_type='application/json;charset=utf-8')
            content = str(f(**kwargs))
            return current_app.response_class(content, mimetype='application/javascript;charset=utf-8')
    return decorated_function

